﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.Device.Location;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;

namespace SpeedRun
{
    public partial class History : PhoneApplicationPage
    {
        Run currentRunDetails = new Run();
        public History()
        {
            InitializeComponent();
            historyFull.LostFocus += new RoutedEventHandler(historyFull_LostFocus);
        }

        void historyFull_LostFocus(object sender, RoutedEventArgs e)
        {
            OnNavigatedTo(new System.Windows.Navigation.NavigationEventArgs(new object(), new System.Uri("/")));
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //List<Run> run = Run.RetrieveHistory();
            //run.Sort(Run.comp);
            //foreach (Run runs in run)
            //{
            //    using (runs)
            //    {
            //        HubTile hubT = new Microsoft.Phone.Controls.HubTile();
            //        hubT.Message = runs.distance + "Mph" + "\n" + runs.calories + "KCal";
            //        hubT.Title = runs.runType + "\n" + runs.time.Minutes.ToString();
            //        hubT.Name = "HubTile " + run.IndexOf(runs);
            //        hubT.Tap += new System.EventHandler<System.Windows.Input.GestureEventArgs>(hubT_Tap);
            //        hubT.Margin = new Thickness(3);
            //        hubT.Width = 200;
            //        hubT.Height = 200;
            //        BitmapImage img = new BitmapImage();
            //        img.CreateOptions = BitmapCreateOptions.None;
            //        img.UriSource = new Uri("/Images/tile2..jpg", System.UriKind.RelativeOrAbsolute);
            //        hubT.Source = img;
            //        wrapPanel.Children.Add(hubT);
            //    }
            //}
        }

        void hubT_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            List<Run> historyRuns = new List<Run>();
            historyRuns = Run.RetrieveHistory();
            var title = (sender as HubTile).Name;
            var hist = historyRuns.ElementAt(Convert.ToInt16(title.Remove(0, 8)));
            try
            {
                historyTime.Text = hist.time.Minutes.ToString() + " mins";
                historyCal.Text = hist.calories + " KCalories";
                historyDistance.Text = hist.distance + " meters";
                historyType.Text = hist.runType.ToString();
                HistortTimeStamp.Text = IsolatedStorageFile.GetUserStoreForApplication().GetCreationTime("History.xml").ToUniversalTime().ToString();
                total.Text = (historyRuns.Count + 1).ToString();
                historyFull.Visibility
                     = System.Windows.Visibility.Visible;
                Notes.Text = (hist.Note == null || hist.Note == String.Empty) ? String.Empty : hist.Note;
                currentRunDetails = hist;
            }
            catch (Exception _e)
            {

            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (historyFull.Visibility == System.Windows.Visibility.Visible)
            {
                historyFull.Visibility = System.Windows.Visibility.Collapsed;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Tasks.ShareStatusTask status = new Microsoft.Phone.Tasks.ShareStatusTask();
            status.Show();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Tasks.BingMapsDirectionsTask task = new Microsoft.Phone.Tasks.BingMapsDirectionsTask();
            task.Start = new Microsoft.Phone.Tasks.LabeledMapLocation("Start", Runner.CoordinatesCovered.FirstOrDefault());
            task.End = new Microsoft.Phone.Tasks.LabeledMapLocation("End", Runner.CoordinatesCovered.LastOrDefault());
            task.Show();

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Run currentRun = currentRunDetails;
            var runs = Run.RetrieveHistory();
            try
            {
                foreach (Run run in runs)
                {
                    if (run.TimeStamp.Equals(currentRun.TimeStamp))
                    {
                        runs.Remove(run);
                    }
                }
            }
            catch (Exception _e_
                )
            {
            
            }
            currentRunDetails.Note = Notes.Text;
            runs.Add(currentRunDetails);
            Run.SaveAll(runs);
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            if (historyFull.Visibility == System.Windows.Visibility.Visible)
                historyFull.Visibility = System.Windows.Visibility.Collapsed;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            wrapPanel.Children.Clear();
            
            List<Run> run = Run.RetrieveHistory();
            run.Sort(Run.comp);
            foreach (Run runs in run)
            {
                using (runs)
                {
                    HubTile hubT = new Microsoft.Phone.Controls.HubTile();
                    hubT.Message = runs.distance + "Mph" + "\n" + runs.calories + "KCal";
                    hubT.Title = runs.runType + "\n" + runs.time.Minutes.ToString();
                    hubT.Name = "HubTile " + run.IndexOf(runs);
                    hubT.Tap += new System.EventHandler<System.Windows.Input.GestureEventArgs>(hubT_Tap);
                    hubT.Margin = new Thickness(3);
                    hubT.Width = 200;
                    hubT.Height = 200;
                    BitmapImage img = new BitmapImage();
                    img.CreateOptions = BitmapCreateOptions.None;
                    img.UriSource = new Uri("/Images/tile2..jpg", System.UriKind.RelativeOrAbsolute);
                    hubT.Source = img;
                    wrapPanel.Children.Add(hubT);
                }
            }
            base.OnNavigatedTo(e);
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            Run currentRun = currentRunDetails;
            var runs = Run.RetrieveHistory();
            try
            {
                foreach (Run run in runs)
                {
                    if (run.TimeStamp.Equals(currentRun.TimeStamp))
                    {
                        runs.Remove(run);
                    }
                }
            }
            catch (Exception _e_
                )
            {

            }

            Run.SaveAll(runs);
        }
    }
}